<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="设备名称" prop="deviceName">
              <el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="设备序列号" prop="sn">
              <el-input v-model="queryParams.sn" placeholder="请输入设备序列号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="视频流地址" prop="url">
              <el-input v-model="queryParams.url" placeholder="请输入视频流地址" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:surveillanceDevice:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['system:surveillanceDevice:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="Delete"
              :disabled="multiple"
              @click="handleDelete()"
              v-hasPermi="['system:surveillanceDevice:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:surveillanceDevice:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="surveillanceDeviceList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column label="主键" align="center" prop="id" v-if="true" /> -->
        <el-table-column label="设备名称" align="center" prop="deviceName" />
        <el-table-column label="设备型号" align="center" prop="model" />
        <el-table-column label="设备序列号" align="center" prop="sn" />
        <el-table-column label="厂商" align="center" prop="manufacturer" />
        <el-table-column label="ip地址" align="center" prop="ip" />
        <el-table-column label="端口" align="center" prop="port" />
        <el-table-column label="用户名" align="center" prop="username" />
        <el-table-column label="密码" align="center" prop="password" />
        <el-table-column label="视频流地址" align="center" prop="url" />
        <el-table-column label="视频编码格式" align="center" prop="videoCodec" />
        <el-table-column label="分辨率" align="center" prop="resolution" />
        <el-table-column label="帧率" align="center" prop="frameRate" />
        <el-table-column label="协议" align="center" prop="protocol" />
        <el-table-column label="安装位置" align="center" prop="location" />
        <el-table-column label="设备状态" align="center" prop="status">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'" size="small">
              {{ scope.row.status === 1 ? '在线' : '离线' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="平台id" align="center" prop="platformId" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:surveillanceDevice:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:surveillanceDevice:remove']"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="查看监控" placement="top">
              <el-button link type="primary" icon="View" @click="handleShow(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改监视设备对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="surveillanceDeviceFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入设备型号" />
        </el-form-item>
        <el-form-item label="设备序列号" prop="sn">
          <el-input v-model="form.sn" placeholder="请输入设备序列号" />
        </el-form-item>
        <el-form-item label="厂商" prop="manufacturer">
          <el-input v-model="form.manufacturer" placeholder="请输入厂商" />
        </el-form-item>
        <el-form-item label="ip地址" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入ip地址" />
        </el-form-item>
        <el-form-item label="端口" prop="port">
          <el-input v-model="form.port" placeholder="请输入端口" />
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="视频流地址" prop="url">
          <el-input v-model="form.url" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="视频编码格式" prop="videoCodec">
          <el-input v-model="form.videoCodec" placeholder="请输入视频编码格式" />
        </el-form-item>
        <el-form-item label="分辨率" prop="resolution">
          <el-input v-model="form.resolution" placeholder="请输入分辨率" />
        </el-form-item>
        <el-form-item label="帧率" prop="frameRate">
          <el-input v-model="form.frameRate" placeholder="请输入帧率" />
        </el-form-item>
        <el-form-item label="协议" prop="protocol">
          <el-input v-model="form.protocol" placeholder="请输入协议" />
        </el-form-item>
        <el-form-item label="安装位置" prop="location">
          <el-input v-model="form.location" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="平台id" prop="platformId">
          <el-input v-model="form.platformId" placeholder="请输入平台id" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog title="设备监控" v-model="shows" id="aa" width="50%" append-to-body>
      <div class="video-container">
        <video
          ref="videoPlayer"
          id="videoPlayer"
          style="width: 100%; height: 100%"
          :src="videoSrc"
          @waiting="onVideoWaiting"
          @playing="onVideoPlaying"
          @error="onVideoError"
          controls
        ></video>
        <div class="box">
          <div class="control">
            <div class="center"></div>
            <div class="o box1">
              <div class="ooo oo">
                <el-icon><TopLeft /></el-icon>
              </div>
              <div class="oo">
                <el-icon><CaretLeft /></el-icon>
              </div>
              <div class="ooo oo">
                <el-icon><BottomLeft /></el-icon>
              </div>
            </div>
            <div class="b box1">
              <div class="oo">
                <el-icon><CaretTop /></el-icon>
              </div>
              <div class="oo"></div>
              <div class="oo">
                <el-icon><CaretBottom /></el-icon>
              </div>
            </div>
            <div class="c box1">
              <div class="ooo oo">
                <el-icon><TopRight /></el-icon>
              </div>
              <div class="oo">
                <el-icon><CaretRight /></el-icon>
              </div>
              <div class="ooo oo">
                <el-icon><BottomRight /></el-icon>
              </div>
            </div>
          </div>
          <div class="speed">
            <div>慢</div>
            <div>中</div>
            <div>快</div>
          </div>
          <div class="cameal">
            <div class="one">相机近焦</div>
            <div class="two">相机远焦</div>
            <div class="thr">相机放大</div>
            <div class="for">相机缩小</div>
          </div>
        </div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="hendleshows">确 定</el-button>
        </div>
      </template></el-dialog
    >
  </div>
</template>

<script setup name="SurveillanceDevice" lang="ts">
import {
  listSurveillanceDevice,
  getSurveillanceDevice,
  delSurveillanceDevice,
  addSurveillanceDevice,
  updateSurveillanceDevice
} from '@/api/device/surveillanceDevice';
import { SurveillanceDeviceVO, SurveillanceDeviceQuery, SurveillanceDeviceForm } from '@/api/device/surveillanceDevice/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const surveillanceDeviceList = ref<SurveillanceDeviceVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);

const total = ref(0);
const videoSrc = ref('your-video-source.mp4');
const isLoading = ref(false);
const loadingMessage = ref('视频加载中...');
const hasError = ref(false);
const queryFormRef = ref<ElFormInstance>();
const surveillanceDeviceFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: SurveillanceDeviceForm = {
  id: undefined,
  deviceName: undefined,
  model: undefined,
  sn: undefined,
  manufacturer: undefined,
  ip: undefined,
  port: undefined,
  username: undefined,
  password: undefined,
  url: undefined,
  videoCodec: undefined,
  resolution: undefined,
  frameRate: undefined,
  protocol: undefined,
  location: undefined,
  status: undefined,
  platformId: undefined
};
const data = reactive<PageData<SurveillanceDeviceForm, SurveillanceDeviceQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deviceName: undefined,
    sn: undefined,
    url: undefined,
    status: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '主键不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '状态 0：离线 1：在线不能为空', trigger: 'change' }]
  }
});
const shows = ref(false);
const { queryParams, form, rules } = toRefs(data);

/** 查询监视设备列表 */
const getList = async () => {
  loading.value = true;
  const res = await listSurveillanceDevice(queryParams.value);
  surveillanceDeviceList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

const handleShow = (row: SurveillanceDeviceVO) => {
  shows.value = true;
  // proxy?.$modal.msgInfo(`监控设备 ${row.deviceName} 的视频流地址为：${row.url}`);
};

const onVideoWaiting = () => {
  isLoading.value = true;
  loadingMessage.value = '视频缓冲中...';
};
const onVideoPlaying = () => {
  isLoading.value = false;
};
const onVideoError = () => {
  isLoading.value = false;
  hasError.value = true;
  loadingMessage.value = '视频加载失败，请刷新重试';
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  surveillanceDeviceFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: SurveillanceDeviceVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加监视设备';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: SurveillanceDeviceVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getSurveillanceDevice(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改监视设备';
};

/** 提交按钮 */
const submitForm = () => {
  surveillanceDeviceFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateSurveillanceDevice(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addSurveillanceDevice(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: SurveillanceDeviceVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除监视设备编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delSurveillanceDevice(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'system/surveillanceDevice/export',
    {
      ...queryParams.value
    },
    `surveillanceDevice_${new Date().getTime()}.xlsx`
  );
};

const hendleshows = () => {
  shows.value = false;
};

onMounted(() => {
  getList();
});
</script>
<style scoped lang="scss">
.video-container {
  position: relative; /* 通常对话框是2000，抽屉需要更高 */

  .box {
    width: 20%;
    height: 100%;
    position: absolute;
    right: 0;
    top: 0;
    background-color: rgba(69, 91, 114);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: white;
    font-size: 14px;
    border-top-left-radius: 40%;
    border-bottom-left-radius: 40%;

    .control {
      width: 100px;
      height: 100px;
      background-image: url('@/assets/images/bc2.jpg');
      background-repeat: no-repeat;
      background-size: cover;
      border-radius: 50%;
      margin-bottom: 20px;
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
      .center {
        width: 30px;
        height: 30px;
        background-image: url('@/assets/images/bc3.jpg');
        background-repeat: no-repeat;
        background-size: cover;
        border-radius: 50%;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
      }
      .box1 {
        width: calc(85% / 3);
        height: 85%;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        align-items: center;
        .oo {
          width: 100%;
          height: calc(100% / 3);
          display: flex;
          justify-content: center;
          align-items: center;
          font-size: 30px;
        }
        .ooo {
          font-size: 16px;
        }
      }
    }

    .speed {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;

      div {
        padding: 0 10px;
        margin: 0 10px;
        background-image: url('@/assets/images/bv4.jpg');
        background-repeat: no-repeat;
        background-size: cover;
        background-position: center;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: rgba(69, 91, 114, 0.8);
        cursor: pointer;

        &:hover {
          background-color: rgba(69, 91, 114, 0.6);
        }
      }
    }

    .cameal {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: 100%;
      margin-top: 20px;
      .one {
        background-image: url('@/assets/images/1.jpg');
      }
      .two {
        background-image: url('@/assets/images/2.jpg');
      }
      .thr {
        background-image: url('@/assets/images/3.jpg');
      }
      .for {
        background-image: url('@/assets/images/4.jpg');
      }
      div {
        font-size: 14px;
        padding: 7px 10px 7px 30px;
        margin: 5px 0;
        background-repeat: no-repeat;
        background-size: cover;
        background-position: center;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: rgba(69, 91, 114, 0.8);
        cursor: pointer;

        &:hover {
          background-color: rgba(69, 91, 114, 0.6);
        }
      }
    }
  }
}
</style>
